package com.wing.financeSet.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wing.financeSet.dao.ProjectCategoryDao;
import com.wing.financeSet.model.entity.ProjectCategory;
import com.wing.financeSet.model.entity.VoucherCategory;
import com.wing.financeSet.model.request.ProjectCategoryForm;
import com.wing.financeSet.model.response.ProjectCategoryVO;
import com.wing.financeSet.model.response.VoucherCategoryVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * 项目分类-ServiceImpl
 *
 * @author
 */
@Service
@Slf4j
public class ProjectCategoryServiceImpl extends ServiceImpl<ProjectCategoryDao, ProjectCategory> implements ProjectCategoryService {

    @Override
    public int add(ProjectCategoryForm projectCategoryForm) {

        //校验

        ProjectCategory projectCategory =new ProjectCategory();
        CopyOptions copyOptions = CopyOptions.create()
        .setEditable(ProjectCategory.class)
        .setIgnoreError(true)
        .setIgnoreNullValue(true);
        BeanUtil.copyProperties(projectCategoryForm, projectCategory, copyOptions);

        return baseMapper.insert(projectCategory);
    }

    @Override
    public List<ProjectCategoryVO> list4app(Map<String, Object> params) {
        return baseMapper.list4app(params);
    }

    @Override
    public Page<ProjectCategoryVO> pageList4app(Page page, Map<String, Object> params) {
        List<ProjectCategoryVO> list = baseMapper.list4app(page, params);
        page.setRecords(list);
        return page;
    }

    @Override
    public ProjectCategoryVO get4appById(Long id) {
        return baseMapper.get4appById(id);
    }

    @Override
    @Transactional
    public int enable(Long id) {
        ProjectCategoryVO projectCategoryVO=baseMapper.get4appById(id);
        if (projectCategoryVO!=null){
            ProjectCategory projectCategory=new ProjectCategory();
            projectCategory.setId(id);
            projectCategory.setIsDeleted(true);
            int updateCount=baseMapper.updateById(projectCategory);
            ProjectCategory projectCategoryParent=new ProjectCategory();
            projectCategoryParent.setId(projectCategoryVO.getParentId());
            projectCategoryParent.setHasEnableChild(true);
            baseMapper.updateById(projectCategoryParent);
            return updateCount;
        }

        return 0;
    }

    @Override
    @Transactional
    public int disable(Long id) {
        ProjectCategoryVO projectCategoryVO=baseMapper.get4appById(id);
        if (projectCategoryVO!=null){
            ProjectCategory projectCategory=new ProjectCategory();
            projectCategory.setId(id);
            projectCategory.setIsDeleted(false);
            int updateCount=baseMapper.updateById(projectCategory);
            int count=baseMapper.selectCount(new QueryWrapper<ProjectCategory>().eq("parent_id",projectCategoryVO.getParentId()).eq("is_deleted",true));
            if (count==0){
                ProjectCategory projectCategoryParent=new ProjectCategory();
                projectCategoryParent.setId(projectCategoryVO.getParentId());
                projectCategoryParent.setHasEnableChild(false);
                baseMapper.updateById(projectCategoryParent);
            }
            return updateCount;
        }

        return 0;
    }
}
